<template>
    <!-- 添加或修改企业对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" :append-to-body="true" custom-class="show-back-class">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目类型" prop="projectType">
          <el-select v-model="form.projectType" placeholder="请选择项目类型" style="width: 100%">
            <el-option class="index-option" v-for="dict in  $store.state.referenceDict.projectType" :key="dict.dictValue"
                       :label="dict.dictLabel" :value="parseInt(dict.dictValue)" />
          </el-select>
        </el-form-item>
        <el-form-item label="启动日：" prop="startDate">
          <el-date-picker v-model="form.startDate" type="date" placeholder="选择启动日" @change="changeStartDate" popper-class="show-back-class date-select-class"></el-date-picker>
        </el-form-item>
        <el-form-item label="移交日：" prop="handoverDate">
          <el-date-picker v-model="form.handoverDate" type="date" placeholder="选择移交日" @change="changeHandoverDate" popper-class="show-back-class date-select-class"></el-date-picker>
        </el-form-item>
        <el-form-item label="空间边界：" prop="spaceBoundary">
          <el-checkbox-group v-model="form.spaceBoundary">
            <el-checkbox label="施工组合（加工）场"></el-checkbox>
            <el-checkbox label="物资设备仓储场"></el-checkbox>
            <el-checkbox label="施工办公区"></el-checkbox>
            <el-checkbox label="施工生活区"></el-checkbox>
            <el-checkbox label="其他">其他</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="数据边界：" prop="dataBoundary">
          <el-checkbox-group v-model="form.dataBoundary">
            <el-checkbox label="化石燃料燃烧排放量"></el-checkbox>
            <el-checkbox label="过程排放量"></el-checkbox>
            <el-checkbox label="购入电力、热力产生的排放量"></el-checkbox>
            <el-checkbox label="碳交易量"></el-checkbox>
            <el-checkbox label="碳抵消量"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="合规标准库：" prop="complianceStandard">
          <el-select v-model="form.complianceStandard" placeholder="选择合规标准">
            <el-option class="index-option" v-for="item in complianceStandardOptions" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
</template>

<script>
import {listProject,getProject, addProject, updateProject} from "@/api/eleLoad/project";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Project",
  components: { Treeselect, },
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        projectName: '',
        projectType: null,
        startDate: '',
        handoverDate: '',
        spaceBoundary: ['施工组合（加工）场','物资设备仓储场','施工办公区','施工生活区','其他'],
        otherSpaceBoundary: '',
        dataBoundary: ['化石燃料燃烧排放量','过程排放量','购入电力、热力产生的排放量','碳交易量','碳抵消量'],
        complianceStandard: 1,
      },
      complianceStandardOptions: [
        {dictLabel:'电力工程建设过程碳排放计算导则',dictValue:1},
      ],
      spaceBoundary: [],
      // 表单校验
      rules: {
        projectName: [{ required: true, trigger: "blur", message: "请输入项目名称" } ],
        projectType: [{ required: true, trigger: "change", message: "请选择项目类型" } ],
        startDate: [{ required: true, trigger: "change", message: "请选择启动日" } ],
        handoverDate: [{ required: true, trigger: "change", message: "请选择移交日" } ],
        complianceStandard: [{ required: true, trigger: "change", message: "请选择合规标准" } ],
        spaceBoundary: [{ required: true, trigger: "change", message: "请选择空间边界" } ],
        dataBoundary: [{ required: true, trigger: "change", message: "请选择数据边界" } ],
        // parentId: [{ required: true, trigger: "change", message: "请选择上级企业" },
        //   { required: true, validator: equalToId, trigger: "change" }],
      },
      menuOptions:[],
    };
  },
  created() {

  },
  methods: {
    changeStartDate(value) {
      this.form.startDate = this.parseTime(value, '{y}-{m}-{d}');
    },
    changeHandoverDate(value) {
      this.form.handoverDate = this.parseTime(value, '{y}-{m}-{d}');
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.projectName,
        children: node.children
      };
    },
    selectEnterpriseDict(){
      listProject().then(response=>{
          this.menuOptions = [];
          const menu = { id: 0, projectName: '主类目', children: [] };
          menu.children = this.handleTree(response.rows);
          this.menuOptions.push(menu);
        });
      },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        projectName: '',
        projectType: null,
        startDate: '',
        handoverDate: '',
        spaceBoundary: ['施工组合（加工）场','物资设备仓储场','施工办公区','施工生活区','其他'],
        otherSpaceBoundary: '',
        dataBoundary: ['化石燃料燃烧排放量','过程排放量','购入电力、热力产生的排放量','碳交易量','碳抵消量'],
        complianceStandard: 1,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.selectEnterpriseDict();
      this.open = true;
      this.title = "添加项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.selectEnterpriseDict();
      this.reset();
      const id = row.projectId || this.ids
      getProject(id).then(response => {
        response.data.dataBoundary = response.data.dataBoundary.split(',');
        response.data.spaceBoundary = response.data.spaceBoundary.split(',');
        this.form = response.data;
        this.open = true;
        this.title = "修改项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$emit('fatherGoPath');
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.projectId) {
            let data = {
              projectId: this.form.projectId,
              projectName: this.form.projectName,
              projectType: this.form.projectType,
              startDate: this.form.startDate,
              handoverDate: this.form.handoverDate,
              spaceBoundary: this.form.spaceBoundary.join(','),
              dataBoundary: this.form.dataBoundary.join(','),
              complianceStandard: this.form.complianceStandard,
            }
            updateProject(data).then(response => {
              if (response.code == 200) {
                this.reset();
                this.open = false;
                this.$message.success('修改成功');
                this.reset();
                this.$emit('fatherGoPath');
                // location.href = '/enterprise';
              } else {
                this.$message.error(response.msg);
              }
            })
          } else {
            let data = {
              projectName: this.form.projectName,
              projectType: this.form.projectType,
              startDate: this.form.startDate,
              handoverDate: this.form.handoverDate,
              spaceBoundary: this.form.spaceBoundary.join(','),
              dataBoundary: this.form.dataBoundary.join(','),
              complianceStandard: this.form.complianceStandard,
            }
            addProject(data).then(response => {
              if (response.code == 200) {
                this.reset();
                this.open = false;
                this.$message.success('新增成功');
                this.reset();
                this.$emit('fatherGoPath');
                // location.href = '/bigScreen/project1';

              } else {
                this.$message.error(response.msg);
              }
            })
          }
        } else {
          this.$message.error("请输入必有参数！");
          return false;
        }
      });
    },
  }
};
</script>
<style>
.date-select-class {
  background-color: rgba(17,93,131,255) !important;
}
</style>